

namespace Orz.LogManager
{
	//========================================================================
	// 	Imported Namespaces
	//========================================================================
	using System;
	using System.IO;
	using System.Collections;
	using System.Text.RegularExpressions;
	using System.Drawing;
	

	///
	/// <summary>
	/// Filters and colour-codes lines of text.
	/// </summary>
	/// <remarks>
	/// This class can be set up to search for a specific pattern in given 
	/// lines of text and to choose a colour based on a match, perform a substitution,
	/// or both.
	/// </remarks>
	///
	public class LineMatch
	{
		//================================================================
		// 	Private Data Members
		//================================================================
		
		/// <summary>The colour to be used if none is specified</summary>
		private static Color oDefaultColour_c = Color.Empty;
		
		/// <summary>The regular expression engine object to use for matching</summary>
		private Regex oRegex_i = null;
		
		/// <summary>The text to use as a replacement in the regular expression</summary>
		private string sReplacementText_i = null;
		
		/// <summary>The colour to paint matched lines</summary>
		private Color oColour_i = Color.Empty;
		
		
		
		//================================================================
		// 	Constructors
		//================================================================

		///
		/// <summary>
		/// Stores the input parameters and creates a new regular expression
		/// instance ready for use.
		/// </summary>
		///
		public LineMatch (string sPattern_p, string sColour_p, string sReplacementText_p)
		{
			oRegex_i = new Regex (sPattern_p, RegexOptions.IgnoreCase | RegexOptions.Multiline);
			
			if (null == sColour_p  ||  0 == sColour_p.Trim().Length  ||  sColour_p.Equals("Normal")  ||  sColour_p.Equals("Default")  ||  sColour_p.Equals("None"))
		    {
		    	oColour_i = Color.Empty;
		    }
		    else
	    	{
		    	oColour_i = Color.FromName(sColour_p);
		    }
		    
		    sReplacementText_i = sReplacementText_p;
		}
		
		
		
		
		//================================================================
		// 	Methods
		//================================================================
		
		///
		/// <summary>
		/// Returns the colour to paint this line.
		/// </summary>
		/// <remarks>
		/// This method uses the regular expression object to check for a match
		/// on the given text and returns the 'match colour' if one is found.
		/// If no match is found, it returns the 'default colour'.
		/// </remarks>
		///
		public Color MatchColour (String sLine_p)
		{
			if (oRegex_i.IsMatch(sLine_p))
			{
				return oColour_i;
			}
			else
			{
				return oDefaultColour_c;
			}
		}
		
		
		
		
		///
		/// <summary>
		/// Performs a search and replace on the text using regular expressions.
		/// </summary>
		///
		public string DoReplacement (String sLine_p)
		{
			if (null != sReplacementText_i)
			{
				return oRegex_i.Replace(sLine_p, sReplacementText_i);
			}
			else
			{
				return sLine_p;
			}
		}
		
		
		
		
		///
		/// <summary>
		/// Determines if this colour description string represents the 'default' colour.
		/// </summary>
		///
		private bool IsEmtpyColourDescription (string sColour_p)
		{
			return (null == sColour_p  ||  
			        0 == sColour_p.Trim().Length  ||  
			        sColour_p.Equals("Normal")  ||  
			        sColour_p.Equals("Default")  ||  
			        sColour_p.Equals("None"));
		}
		
		
	} // end class
}
	
